package com.rz.paas.test

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import com.orhanobut.hawk.Hawk
import com.rz.paas.test.base.MiddleActivity
import com.rz.paas.test.constant.AppConstant
import com.rz.paas.test.inChannel.InChannelActivity
import com.rz.paas.test.inChannel.viewModel.CommunicationMode
import com.rz.paas.test.viewPager.BannerAdapter
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : MiddleActivity() {
    //    fun open(context: Context) {
//        val intent = Intent(context, LoginActivity::class.java)
//        context.startActivity(intent)
//    }
    private val TAG = "LoginActivity"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        generateView()

        login_btn.setOnClickListener {
            val checkNull = checkNull(channel_id_tv, channel_id)

            if (checkNull) {
                Log.d(TAG, "onCreate: selectMode:$selectMode")
                Hawk.put(AppConstant.COMMUNICATION_MODE, selectMode)
                Hawk.put(AppConstant.CHANNEL_ID, channel_id_tv.text.toString())
                Hawk.put(AppConstant.USER_ID, user_id_tv.text.toString())
                InChannelActivity.open(this, selectMode)
            }
        }
    }

    override fun onResume() {
        super.onResume()
        channel_id_tv.setText(Hawk.get(AppConstant.CHANNEL_ID, ""))
        user_id_tv.setText(Hawk.get(AppConstant.USER_ID, ""))
    }

    private fun checkNull(ipAddressTv: TextInputEditText, inputLayout: TextInputLayout): Boolean {
        if (ipAddressTv.text.toString().isEmpty()) {
            inputLayout.error = "格式错误"
            return false
        }
        return true
    }

    var selectMode = CommunicationMode.COMMUNICATION_MODE
    private fun generateView() {

        banner_view.setAdapter(bannerAdapter)
        banner_view.post {
            var getIndex = Hawk.get(AppConstant.COMMUNICATION_MODE, 0)
            Log.d(TAG, "generateView: getIndex:$getIndex")
            banner_view.setPageIndex(getIndex)
        }

    }

    val bannerAdapter = object : BannerAdapter() {
        override fun getView(position: Int, convertView: View?): View {
            if (convertView == null) {
                val layoutId = when (position) {
                    CommunicationMode.COMMUNICATION_MODE -> {
                        R.layout.communication_mode_layout
                    }
                    CommunicationMode.LIVE_MODE_ANCHOR -> {
                        R.layout.live_mode_author_layout
                    }
                    else -> {
                        R.layout.live_mode_audience_layout
                    }
                }
                return LayoutInflater.from(baseContext)
                    .inflate(layoutId, banner_view, false)
            }

            return convertView
        }

        override fun getCount(): Int {
            return 3
        }

        override fun onPageChanged(position: Int) {
            selectMode = position
        }
    }

    fun jumpToSettingActivity(view: View) {

        startActivity(Intent(baseContext, SettingActivity::class.java))
    }
}